package util

import (
	"fmt"
	"net"
	"strconv"
	"time"
)

func Float64Decimal2(v float64) float64 {
	value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v), 64)
	return value
}

var GStartTime time.Time = time.Now()

func RunTime() uint64 {
	return uint64(time.Since(GStartTime).Seconds())
}

// GetLocalIP 服务端ip
func LocalIP() string {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return ""
	}
	for _, addr := range addrs {
		ipAddr, ok := addr.(*net.IPNet)
		if !ok {
			continue
		}
		if ipAddr.IP.IsLoopback() {
			continue
		}
		if !ipAddr.IP.IsGlobalUnicast() {
			continue
		}
		return ipAddr.IP.String()
	}
	return ""
}
